import { request } from '../../request/index.js';
Page({
  data: {
    //左边菜单数据
    leftMenuList:[],
    //右边的数据
    rightContent:[],
    //记录当前的索引
    currentIndex:0,
    //scrollTop
    scrollTop:0
  },
  //接口返回的数据
  Cates:[],
  onLoad: function (options) {
    //发送前，需要判断一些有没有本地存储数据，有的话就不用发送了
    // this.getCates();

    //获取本地存储的数据
    const Cates = wx.getStorageSync('cates');
    //判断
    if(!Cates) {
      this.getCates();
    }else {
      //判断数据有没有过期
      if(Date.now() - Cates.time > 1000*10) {
        this.getCates();
      }else {
        this.Cates = Cates.data;
        let leftMenuList = this.Cates.map(v => v.cat_name);
        let rightContent = this.Cates[0].children;
        this.setData({
          leftMenuList,
          rightContent,
          scrollTop:-1
        })
      }
    }
  },
  getCates() {
    request({url:"/categories"})
      .then(result => {
          this.Cates = result.data.message;
          //把接口数据存储到本地存储中
          wx.setStorageSync('cates', {time:Date.now(),data:this.Cates});
          //为左边数据赋值
          let leftMenuList = this.Cates.map(v => v.cat_name);
          let rightContent = this.Cates[0].children;
          this.setData({
            leftMenuList,
            rightContent,
            scrollTop:-1
          })
      })
  },
  //处理点击事件
  handleItemtap(e) {
    //改变当前的currentIndex 为 index
    let {index} = e.currentTarget.dataset;
    this.setData({
      currentIndex:index
    });
    //
    let rightContent = this.Cates[index].children;
    this.setData({
      rightContent
    })
  }
})